package com.hg.miaosha.common.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: skh
 * @Date: 2019/6/17 15:34
 * @Description: 切面锁
 */
@Component
@Aspect
@Order(1)
//order越小越是最先执行，但更重要的是最先执行的最后结束。
public class LockAspect {


    private static Lock lock = new ReentrantLock(true);

    @Pointcut("@annotation(com.hg.miaosha.common.aop.Servicelock)")
    public void lockAspect() {

    }

    @Around("lockAspect()")
    public Object around(ProceedingJoinPoint point) {
        lock.lock();
        Object obj = null;
        try {
            obj = point.proceed();
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }finally {
            lock.unlock();
        }

        return obj;
    }


}
